import axios from "axios";

//创建实例
const instance = axios.create({
  //超时设置
  timeout: 10000,
});

//响应拦截
instance.interceptors.response.use(
  (res) => res.data,
  (err) => Promise.reject(err)
);

//封装请求方法

//get请求
export const get = (url, config = {}) => {
  return instance.get(url, config);
};

//post请求
export const post = (url, data = {}, config = {}) => {
  if ({}.toString.call(data) !== "[object Object]") {
    throw new Error("类型不合法");
  }
  return instance.post(url, data, config);
};

export const post2 = (url, data, config) => {
  return fetch(url, {
    method: "post",
    body: JSON.stringify(data),
    headers: {
      "content-type": "application/json",
    },
  })
    .then((ret) => ret.json())
    .then((data) => data);
};

export const post4 = async (url, data, config) => {
  return await (
    await fetch(url, {
      method: "post",
      body: JSON.stringify(data),
      headers: {
        "content-type": "application/json",
      },
    })
  ).json();
};
